package com.music.urlValidator;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.beans.factory.annotation.Required;

import com.music.urlValidator.beans.ConnectionSettings;
import com.music.urlValidator.beans.ProxySettings;
import com.music.urlValidator.constants.ContentType;

public class MusicValidatorImpl implements MusicValidator {

	private ConnectionSettings mConnectionSettings;

	public MusicValidatorImpl() {
		
	}

	public MusicValidatorImpl(ConnectionSettings pConnectionSettings) {
		mConnectionSettings = pConnectionSettings;
	
	}

	public boolean isValid(String pUrl) throws IOException, HttpException {
		HttpClient mHttpClient = new HttpClient();

		GetMethod method = new GetMethod(pUrl);

		// Provide custom retry handler is necessary
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler(1, false));

		try {
			
			mHttpClient.getParams().setConnectionManagerTimeout(getConnectionTimeOut());
			mHttpClient.getParams().setSoTimeout(getResponseTimeOut());

			//Set Proxy Settings.
			setProxySettings(mHttpClient);
			// Execute the method.
			int statusCode = mHttpClient.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + method.getStatusLine());
				throw new HttpException(method.getStatusLine().getReasonPhrase());
			}
			
			String contentType = method.getResponseHeader("content-Type").getValue();
			
			System.out.println(contentType);
			
			if(contentType.equalsIgnoreCase(ContentType.AUDIO_MPEG.getType())){
				return true;
			}
			
		} catch (HttpException e) {
			System.err.println("Fatal protocol violation: " + e.getMessage());
			//log the exception.
			throw e;
		} catch (IOException e) {
			System.err.println("Fatal transport error: " + e.getMessage());
			//log the exception.
			throw e;
		} finally {
			// Release the connection.
			method.releaseConnection();
		}
		return false;
	}

	/**
	 * @return the connectionSettings
	 */
	public ConnectionSettings getConnectionSettings() {
		return mConnectionSettings;
	}

	/**
	 * @param pConnectionSettings the connectionSettings to set
	 */
	@Required
	public void setConnectionSettings(ConnectionSettings pConnectionSettings) {
		mConnectionSettings = pConnectionSettings;
	}

	private int getConnectionTimeOut(){
		return getConnectionSettings().getConnectionTimeOut();
	}
	private int getResponseTimeOut(){
		return getConnectionSettings().getResponseTimeOut();
	}
	private void setProxySettings(HttpClient pHttpClient){
		if(getConnectionSettings().getProxySettings() != null){
			ProxySettings proxy = getConnectionSettings().getProxySettings();
			if(proxy.isProxyEnabled()){
				pHttpClient.getHostConfiguration().setProxy(proxy.getHostName(),proxy.getPortNo());
			}
		}
	}	
		

}
